package com.example.lenovo.demo.presenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.MapView;
import com.bumptech.glide.Glide;
import com.example.lenovo.demo.CityActivity;
import com.example.lenovo.demo.MapActivity;
import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.databean.MainMapBean;

import java.util.List;

/**
 * Created by Lenovo on 2017/1/13.
 */

public class MainMapActicityRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Object> data;
    private Context context;

    public MainMapActicityRvAdapter(List<Object> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder=null;
        switch (viewType){
            case 0:
                viewHolder=new HeadVh(LayoutInflater.from(context).inflate(R.layout.mainmaprvhead,parent,false));
                break;
            case 1:
                viewHolder=new SubHeadVh(LayoutInflater.from(context).inflate(R.layout.mainmapactivity_rvsubhead,parent,false));
                break;
            case 2:
                viewHolder=new ItemVh(LayoutInflater.from(context).inflate(R.layout.mainmap_rvitem,parent,false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(data.get(position) instanceof MainMapBean.DataBean.DestinationBean){
            MainMapBean.DataBean.DestinationBean destinationBean= (MainMapBean.DataBean.DestinationBean) data.get(position);
            HeadVh headVh= (HeadVh) holder;
            headVh.textView.setText(destinationBean.getDestinations().size()+"个目的地");

        }else if (data.get(position) instanceof String){
            SubHeadVh subHeadVh= (SubHeadVh) holder;
            String name= (String) data.get(position);
            subHeadVh.textView.setText(name);
        }else {
            final ItemVh itemVh= (ItemVh) holder;
            MainMapBean.DataBean.GroupingsBean.SectionsBean sectionsBean= (MainMapBean.DataBean.GroupingsBean.SectionsBean) data.get(position);
            MainMapBean.DataBean.GroupingsBean.SectionsBean.DestinationBeanX destinationBeanX=sectionsBean.getDestination();
            itemVh.title.setText(destinationBeanX.getName()+"·"+destinationBeanX.getTitle());
            itemVh.subTitle.setText(destinationBeanX.getVisit_tip());
            itemVh.contents.setText(destinationBeanX.getDescription());
            Glide.with(context).load(destinationBeanX.getPhoto_url())
                    .into(itemVh.imageView);
            itemVh.linearLayouttitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(itemVh.linearLayout.getVisibility()==View.VISIBLE){
                        itemVh.linearLayout.setVisibility(View.GONE);
                    }else {
                        itemVh.linearLayout.setVisibility(View.VISIBLE);
                    }

                }
            });
            itemVh.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,"这个地图还没写",Toast.LENGTH_SHORT).show();
//                    MainMapBean.DataBean.DestinationBean destinationBean= (MainMapBean.DataBean.DestinationBean) data.get(0);
//                    Intent intent=new Intent(context, CityActivity.class);
//                    intent.putExtra("id",destinationBean.getId()+"");
//                    context.startActivity(intent);
                }
            });

        }



    }

    @Override
    public int getItemViewType(int position) {
        if(data.get(position) instanceof MainMapBean.DataBean.DestinationBean){
            return 0;
        }else if (data.get(position) instanceof String){
            return 1;
        }else {
            return 2;
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class HeadVh extends RecyclerView.ViewHolder{
        MapView mapView;
        TextView textView;
        public HeadVh(View itemView) {
            super(itemView);
            mapView= (MapView) itemView.findViewById(R.id.mainmap_head_mapview);
            textView= (TextView) itemView.findViewById(R.id.mainmap_head_tv);
        }
    }

    class SubHeadVh extends RecyclerView.ViewHolder{
        TextView textView;
        public SubHeadVh(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.MainMap_RvItem_tv);
        }
    }

    class ItemVh extends RecyclerView.ViewHolder{
        TextView title,subTitle,contents;
        ImageView imageView;
        Button button;
        LinearLayout linearLayout,linearLayouttitle;
        public ItemVh(View itemView) {
            super(itemView);
            title= (TextView) itemView.findViewById(R.id.mainmap_parent_tv1);
            subTitle= (TextView) itemView.findViewById(R.id.mainmap_parent_tv2);
            linearLayouttitle= (LinearLayout) itemView.findViewById(R.id.mainmap_rvitem_linearlayout);
            linearLayout= (LinearLayout) itemView.findViewById(R.id.cityac_lvitem_includeLinearLayout);
            contents= (TextView) itemView.findViewById(R.id.mainmap_child_tv);
            imageView= (ImageView) itemView.findViewById(R.id.mainmap_child_img);
            button= (Button) itemView.findViewById(R.id.mainmap_child_btn);
        }
    }
}
